from Base.ApiBase import ApiBase
import pytest,yaml

def read_yaml():
    with open(r'C:\Users\LENOVO\PycharmProjects\AutoWMS\yuelian2\data\data.yaml',encoding='utf-8') as f:
        return yaml.safe_load(f)

def read_yaml1():
    with open(r'C:\Users\LENOVO\PycharmProjects\AutoWMS\yuelian\data\data1.yaml',encoding='utf-8') as f:
        return yaml.safe_load(f)

class Test_Api():
    token = None
    a = None
    @pytest.fixture(scope='session')
    def api(self):
        return ApiBase('http://10.59.9.12:7001/')

    @pytest.mark.parametrize('data',read_yaml())
    def test_01(self,api,data):
        body = {"username":data['username'],"password":data['password']}
        login = api.post(
            path='api/public/login',
            json=body
        )
        print(login.json())
        if data['exp'] == '登录成功':
            Test_Api.token = 'Bearer ' + login.json()['data']['token']
        elif data['exp'] == '登录失败':
            print('失败')

    def test_02(self,api):
        tou = {'auth-token':Test_Api.token}
        a = api.get(
            path='api/ucenter/info',
            headers=tou
        )
        print(a.json())

    def test_03(self,api):
        tou = {'auth-token':Test_Api.token}
        a = api.get(
            path='api/second?page=1&type=0&sort=1',
            headers=tou
        )
        print(a.json())
        Test_Api.a = a.json()['data']['list'][0]['id']

    @pytest.mark.parametrize('data',read_yaml1())
    def test_04(self,api,data):
        tou = {'auth-token':Test_Api.token}
        body = {"id":data['id'],"type":"second","address_id":data['address'],"store_id":2,"shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":"http://10.59.9.12:7001/wap/pages/cart/confirm?id=23&sku_id=17&num=1&type=second","sku_id":"17","num":"1"}
        a = api.post(
            path='api/order/create',
            json=body,
            headers=tou
        )
        print(a.json())

if __name__ == '__main__':
    pytest.main()